/*
 * This class will detail the constants
 * of the player's sprite.  Will extend
 * the sprite class.  The player will be able
 * to rotate the sprite with the arrow keys
 * as well as thrust which increases the ship's 
 * velocity in the direction it is facing.
 */
package com.PortalAsteroidjava;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.files.FileHandle;

public class Player extends Sprite{
	//The size of the ship
	public static final int SIZE = 32;
	
	//The angle of each step of rotation in degrees (if the ship is rotating)
	public static final int ROTATION = 10;
	
	//Change in velocity for each step while thrusting
	public static final double THRUST_STEP = 0.2;
	
	//The square of the ship's max speed
	public static final double MAX_SPEED = 9.0;
	
	//The speed of a missile launched from the ship
	public static final double MISSILE_SPEED = 4.0;
	
	//The cooldown for the ship's grapple ability (in seconds)
	private int grappleCooldown;
	
	//The angle that the ship is facing
	private double shipAngle;

	
	/**
	 * This method creates a ship.  Since this is the player's
	 * ship, it will always start at the same position with the same
	 * initial velocity (zero) and the same width and height.
	 * The ship will initially face the positive x-axis
	 * and the life count will default to 3.
	 */
	
	public Player(float x, float y){
		super(PortalAsteroidjava.playerTexture, SIZE, SIZE);
		
		this.setPosition(x, y);
		this.shipAngle = 0.0;
	}
	
	public void move(float x, float y)
	{
		this.setPosition(this.getX() + x, this.getY() + y);
	}
	
	/**
	 * Handles a collision between the player's ship
	 * and any of the obstacles he must avoid, including
	 * enemy ships, asteroids, and enemy ship projectiles. 
	 * Takes in the Sprite the player has collided with as 
	 * an argument.
	 */
	
	/*public void collision(Sprite s)
	{
		//TO DO
	}*/
	
	/**
	 * Bounces the ship off of the side of the screen
	 * if the ship runs out of bounds.
	 * 
	 * @param x1 the x-coordinate of one corner of the bounding box
	 * @param y1 the y-coordinate of one corner of the bounding box
	 * @param x2 the x-coordinate of the opposite corner of the bounding box
	 * @param y2 the y-coordinate of the opposite corner of the bounding box
	 */
	
	/*public void outOfBounds(int x1, int y1, int x2, int y2)
	{
		if (this.position.x < x1)
		{
			this.position.x = x1 + (x1 - this.position.x);
			this.velocity.x = -this.velocity.x;
		}
		else if (this.position.x >= x2)
		{
			this.position.x = x2 - 1 - (this.position.x - x2);
			this.velocity.x = -this.velocity.x;
		}
		
		if (this.position.y < y1)
		{
			this.position.y = y1 + (y1 - this.position.y);
			this.velocity.y = -this.velocity.y;
		}
		else if (this.position.y >= y2)
		{
			this.position.y = y2 - 1 - (this.position.y - y2);
			this.velocity.y = -this.velocity.y;
		}
	}*/
	
	/**
	 * Rotates this ship in the given direction
	 * 
	 * @param dir -1 for counterclockwise, 1 for clockwise
	 */
	
	/*public void rotate(int dir)
	{
		shipAngle += dir * ROTATION;
		
		//Keep the rotation in the 0 to 360 degees range
		if (shipAngle < 0.0)
			shipAngle += 360.0;
		else if (shipAngle > 360.0)
			shipAngle -= 360.0;
	}*/
	
	/**
	 * Moves the ship
	 */
	
	/*public void tick()
	{
		super.tick();
	}*/
	
	/**
	 * Increases the velocity of this ship in the direction
	 * the player is currently facing.
	 */
	
	/*public void thrust()
	{
		//update velocity
		
		this.velocity.x += Math.sin(Math.toRadians(shipAngle)) * THRUST_STEP;
		this.velocity.y += Math.cos(Math.toRadians(shipAngle)) * THRUST_STEP;
		
		//But scale back if it exceeds max
		double v = this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y;
		
		if (v > MAX_SPEED)
		{
			double scale = Math.sqrt(v) / Math.sqrt(MAX_SPEED);
			
			this.velocity.x /= scale;
			this.velocity.y /= scale;
		}
	}*/
	
	/**
	 * Fires a missile in the direction the player is facing.
	 */
	
	/*public void shoot()
	{
		//precompute some trig
		
		double sine = Math.sin(Math.toRadians(shipAngle));
		double cosine = Math.cos(Math.toRadians(shipAngle));
		
		int midX = (int)getMidX();
		int midY = (int)getMidY();
		
		//Place the missile some distance from the ship
		//so it doesn't immediately collide.
		int missileX = (int)(midX + (SIZE + 2) * sine / 2);
		int missileY = (int)(midY + (SIZE + 2) * cosine / 2);
		
		//create the missile and add it to the game
		Game.addMissile(new Missile(missileX, missileY, 
						MISSILE_SPEED * sine, 
						MISSILE_SPEED * cosine));
		
		
	}*/
	
	/**
	 * Returns the speed of the missiles the ship fires
	 */
	
	/*public double getMissileSpeed()
	{
		return MISSILE_SPEED;
	}*/
	
	/**
	 * Returns the x and y coordinate of this ship's midpoint
	 */
	
	/*public double getMidX()
	{
		return this.position.x + SIZE / 2.0;
	}
	
	public double getMidY()
	{
		return this.position.y + SIZE / 2.0;
	}*/
}

